package com.example.springboot01.resp;

import com.example.springboot01.resp.enums.RespEnum;
import lombok.Data;

@Data
public class Response {

    public static final int SUCCESS_CODE = 200;

    public static final int ERROR_CODE = 500;

    private Integer code;

    private String msg;

    private Object data;


    public Response(Integer code, String msg, Object data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }


    public Response(RespEnum respEnum, Object data) {
        this(respEnum.getCode(), respEnum.getMsg(), data);
    }


    public Response(RespEnum respEnum) {
        this(respEnum, null);
    }

    public Response() {

    }

    public Response setMsg(String msg) {
        this.msg = msg;
        return this;
    }

    public static Response response(RespEnum respEnum, Object data) {
        return new Response(respEnum, data);
    }

    public static Response ok(Object data) {
        return new Response(SUCCESS_CODE, "", data);
    }

    public static Response error(String msg) {
        return new Response(ERROR_CODE, msg, null);
    }

    public static Response error(int code, String msg) {
        return new Response(code, msg, null);
    }


    public static Response response(RespEnum respEnum) {
        return response(respEnum, null);
    }
}
